//
//  RankingListViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/22.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "RankingListViewController.h"
#import "NavigationBarToggleTabView.h"
#import "RHTableView.h"
#import "ButtonPullDown.h"
#import "SelectTypeView.h"
#import "MarketingPHBTableViewCell.h"

@interface RankingListViewController ()<SelectTypeViewDelegate>{
    RHTableView *table_user;
    
    ButtonPullDown *btnType1;//月份
    ButtonPullDown *btnType2;//季度
    ButtonPullDown *btnType3;//年份
    UIButton *btnType4;//累计
    
    SelectTypeView *selectType;
    NSMutableDictionary *selectDicType;
    NSString *strTempType;
    
    NSDictionary *dicDateType;
    ButtonPullDown *btnTemp;
}

@end

@implementation RankingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    RegisterNotify(@"againLoadUserData", @selector(againLoadUserData))
    selectDicType=[NSMutableDictionary new];
    [self navbarTitle:@" "];
    [self initTopTypeButtons];
    [self initTabelViews];
    [self initNavTopButtons];
}
-(void)initNavTopButtons{
    NavigationBarToggleTabView *toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-225)/2, 28, 225, 28)];
    [self.navView addSubview:toggleTab];//类型（1员工 2部门 3公司）默认显示员工
    __weak RankingListViewController *viewC=self;
    [toggleTab setTiltelArray:@[@{@"title":@"个人",@"id":@"1"},@{@"title":@"部门",@"id":@"2"},@{@"title":@"子公司",@"id":@"3"}] toggleTab:^(NSDictionary *dicSelect) {
        strTempType=[dicSelect valueForJSONStrKey:@"id"];
        [viewC loadTableData];
        if ([[dicSelect valueForJSONStrKey:@"id"] isEqualToString:@"1"]) {
            
        }else{
            
        }
    }];
}
-(void)initTopTypeButtons{
    UIView *viewTypesBG=[[UIView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, 44)];
    viewTypesBG.backgroundColor=rgbGray;
    [self.view addSubview:viewTypesBG];
    
    
    
    //----------------------------------type---------------------------------------
    
    float fw=(kScreenWidth-50)/4.0;
    //-----------------------------类型-----------------------------
    btnType1=[[ButtonPullDown alloc] initWithFrame:CGRectMake(10, 10, fw, 24) withSel:@selector(sampleTypeButtonClicked:) addTarget:self buttonTitle:@"月份"];
    [viewTypesBG addSubview:btnType1];
    btnType2=[[ButtonPullDown alloc] initWithFrame:CGRectMake(XW(btnType1)+10, 10, fw, 24) withSel:@selector(sampleTypeButtonClicked:) addTarget:self buttonTitle:@"季度"];
    [viewTypesBG addSubview:btnType2];
    btnType3=[[ButtonPullDown alloc] initWithFrame:CGRectMake(XW(btnType2)+10, 10, fw, 24) withSel:@selector(sampleTypeButtonClicked:) addTarget:self buttonTitle:@"年份"];
    [viewTypesBG addSubview:btnType3];

    btnType4=[RHMethods buttonWithFrame:CGRectMake(XW(btnType3)+10, 10, fw, 24) title:@"累计" image:nil bgimage:nil];
    [btnType4 setBackgroundColor:[UIColor whiteColor]];
    [btnType4 addTarget:self action:@selector(TotalButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [viewTypesBG addSubview:btnType4];
    
    btnType1.selected=YES;
    [btnType1 setImage:[UIImage imageNamed:@"phbxsjon"] forState:UIControlStateSelected];
    [[Utility Share] viewLayerRound:btnType1 borderWidth:0.5 cornerRadius:3 borderColor:rgbpublicColor];
    
    
    [[Utility Share] viewLayerRound:btnType2 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    [[Utility Share] viewLayerRound:btnType3 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    [[Utility Share] viewLayerRound:btnType4 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    
    btnType1.titleLabel.font=fontTxtContent;
    btnType2.titleLabel.font=fontTxtContent;
    btnType3.titleLabel.font=fontTxtContent;
    btnType4.titleLabel.font=fontTxtContent;
    
    [viewTypesBG addSubview:[RHMethods lineViewWithFrame:CGRectMake(0, 43.5, kScreenWidth, 0.5)]];


}

-(void)initTabelViews{
    table_user=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight+44, kScreenWidth, kContentHeight-44)];
    [table_user showRefresh:YES LoadMore:YES];
    table_user.delegate = self;
    table_user.dataSource = self;
    [table_user setBackgroundColor:rgbGray];
    [table_user setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:table_user];
    [table_user setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [table_user registerClass:[MarketingPHBTableViewCell class] forCellReuseIdentifier:@"MarketingPHBTableViewCell"];
}


#pragma mark selector
-(void)againLoadUserData{
    dicDateType=nil;
    [self loadTableData];
}
-(void)loadDateTypeData{
    krequestParam
    [NetEngine createGetAction_LJ_two:[NSString stringWithFormat:@"%@%@",XKFV3userdate,dictparam.wgetParamStr] onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            dicDateType=[resData objectForJSONKey:@"data"];
            [self sampleTypeButtonClicked:btnTemp];
        }else{
            [self loadDateTypeData];
        }
    } onError:^(NSError *error) {
        [self loadDateTypeData];
    }];
    
    
}
-(void)loadTableData{
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    [dictparam setValue:strTempType forKey:@"type"];
    if ([[selectDicType valueForJSONStrKey:@"leiji"] notEmptyOrNull]) {
        [dictparam setValue:@"1" forKey:@"all"];
    }else{
        if ([selectDicType objectForJSONKey:@"month"]) {
            [dictparam setValue:[[selectDicType objectForJSONKey:@"month"] valueForJSONStrKey:@"id"] forKey:@"month"];
        }
        if ([selectDicType objectForJSONKey:@"quarter"]) {
            [dictparam setValue:[[selectDicType objectForJSONKey:@"quarter"] valueForJSONStrKey:@"id"] forKey:@"quarter"];
        }
        if ([selectDicType objectForJSONKey:@"year"]) {
            [dictparam setValue:[[selectDicType objectForJSONKey:@"year"] valueForJSONStrKey:@"id"] forKey:@"year"];
        }
    }
    
    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV3usertop,dictparam.wgetParamStr];
    [table_user refresh];
}
#pragma mark button
-(void)TotalButtonClicked{
    [self refreshButtonSelectStatus];
    [btnType1 setButtonTitle:@"月份"];
    [btnType2 setButtonTitle:@"季度"];
    [btnType3 setButtonTitle:@"年份"];
    [selectDicType removeAllObjects];
    [selectDicType setValue:@"1" forKey:@"leiji"];
    btnType4.selected=YES;
    [[Utility Share] viewLayerRound:btnType4 borderWidth:0.5 cornerRadius:3 borderColor:rgbpublicColor];
    [self loadTableData];
}
-(void)sampleTypeButtonClicked:(ButtonPullDown *)btn{
    btnTemp=btn;
    if (!dicDateType) {
        [self loadDateTypeData];
        return;
    }
    if (!selectType) {
        selectType=[[SelectTypeView alloc] init];
        selectType.delegate=self;
    }
    [self refreshButtonSelectStatus];
    selectType.floatTypeY=Y(table_user);
    [btn setImage:[UIImage imageNamed:@"menuiconup"] forState:UIControlStateSelected];
    btn.selected=YES;
    [[Utility Share] viewLayerRound:btn borderWidth:0.5 cornerRadius:3 borderColor:rgbpublicColor];
    [selectDicType removeObjectForKey:@"leiji"];
    NSString *strType;
    NSArray *arrayType;
    if (btn==btnType1) {
        strType=@"month";
        arrayType=[dicDateType objectForJSONKey:@"month"];
    }else if (btn==btnType2){
        strType=@"quarter";
        arrayType=[dicDateType objectForJSONKey:@"quarter"];
    }else if (btn==btnType3){
        strType=@"year";
        arrayType=[dicDateType objectForJSONKey:@"year"];
    }
    selectType.strType=strType;
    [selectType showAllArray:arrayType selectDic:[selectDicType objectForJSONKey:strType]];
}
-(void)refreshButtonSelectStatus{
    btnType1.selected=NO;
    btnType2.selected=NO;
    btnType3.selected=NO;
    btnType4.selected=NO;
    
    [[Utility Share] viewLayerRound:btnType1 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    [[Utility Share] viewLayerRound:btnType2 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    [[Utility Share] viewLayerRound:btnType3 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    [[Utility Share] viewLayerRound:btnType4 borderWidth:0.5 cornerRadius:3 borderColor:rgbLineColor];
    

}
#pragma mark SelectTypeViewDelegate
-(void)selectTypeView:(SelectTypeView *)sview object:(NSDictionary *)dic{
    [selectDicType setValue:dic forKey:sview.strType];
    if ([sview.strType isEqualToString:@"month"]) {
        [btnType1 setButtonTitle:[dic valueForJSONStrKey:@"name"]];
        [selectDicType removeObjectForKey:@"quarter"];
        [btnType2 setButtonTitle:@"季度"];
    }else if ([sview.strType isEqualToString:@"quarter"]){
        [btnType2 setButtonTitle:[dic valueForJSONStrKey:@"name"]];
        [selectDicType removeObjectForKey:@"month"];
        [btnType1 setButtonTitle:@"月份"];
    }else if ([sview.strType isEqualToString:@"year"]){
        [btnType3 setButtonTitle:[dic valueForJSONStrKey:@"name"]];
    
    }
    [self refreshButtonSelectStatus];
    [self loadTableData];
}
-(void)selectTypeViewHidden:(SelectTypeView *)sview{
    [self refreshButtonSelectStatus];
//    if ([sview.strType isEqualToString:@"month"]) {
//        [btnType1 setImage:[UIImage imageNamed:@"phbxsjon"] forState:UIControlStateSelected];
//    }else if ([sview.strType isEqualToString:@"quarter"]){
//        [btnType2 setImage:[UIImage imageNamed:@"phbxsjon"] forState:UIControlStateSelected];
//    }else if ([sview.strType isEqualToString:@"year"]){
//        [btnType3 setImage:[UIImage imageNamed:@"phbxsjon"] forState:UIControlStateSelected];
//    }
}
#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return table_user.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];    
    MarketingPHBTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MarketingPHBTableViewCell" forIndexPath:indexPath];
    [cell setValueForDictionary:dic indexPath:indexPath type:self.userInfo];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
